from fastapi import APIRouter, Depends
from typing import List, Any
from schemas.common import RETURN, ListRequest

from core.Depend import has_permission, check_token
import models.admin.system.Admin as AdminModel
from server.Base import BaseServer


router = APIRouter(prefix="/operation", tags=["系统监管"])


# 查询登录日志
@router.post("/logs",summary="查询操作日志",response_model=RETURN[Any])
async def get_login_logs(request:ListRequest,user: AdminModel.redis_admin = Depends(has_permission("system_monitor_operation_logs_list"))):
  data = await BaseServer(AdminModel.Admin_Record).list(request)
  return RETURN.success("查询成功",data)

# 查询当前登录用户的操作日志
@router.post("/current",summary="查询当前登录用户的操作日志",response_model=RETURN[Any])
async def get_current_login_logs(request:ListRequest,user: AdminModel.redis_admin = Depends(check_token)):
  # 判断 request.filter中是否有admin_id
  if "admin_id" in request.filter:
    request.filter["admin_id"] = user.admin.id
  data = await BaseServer(AdminModel.Admin_Record).list(request)
  return RETURN.success("查询成功",data)

# 批量删除登录日志
@router.delete("/batch_delete",summary="批量删除登录日志",response_model=RETURN[Any])
async def delete_login_logs(ids: List[int],user: AdminModel.redis_admin = Depends(has_permission("system_monitor_operation_logs_del_batch"))):
  await BaseServer(AdminModel.Admin_Record).batch_delete(ids)
  return RETURN.success("删除成功")

# 清楚登录日志
@router.delete("/clear",summary="清楚登录日志",response_model=RETURN[Any])
async def clear_login_logs(user: AdminModel.redis_admin = Depends(has_permission("system_monitor_operation_logs_del_all"))):
  await BaseServer(AdminModel.Admin_Record).clear()
  return RETURN.success("清空成功")